/**
 *  device.h -
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __DEVICE_H__
#define __DEVICE_H__

#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/list.h"
#include "include/mem.h"
#include "include/sem.h"
#include "include/irq.h"
#include "utility/bstring.h"
#endif


typedef struct _device {
    list_t link;
    sem_t mutex;             // Multitask protected.
    int32 used_cnt;         // Use count.
    char_t *name;            // Device name.
    uint8 mid;              // Master ID.
    uint8 sid;              // Slave ID.
    uint8 dev_type;         // Device type.
    bool_t delete_flag;      // Delete flag that indicates the device will be released.
    void (*release)(void);   // The function called when release a device.
} device_t;

#define INVALID_ID (0xFF)

// For dev_type
#define CHDEV 0x01  // Char device.
#define BLKDEV 0x02 // Block device.
#define NETDEV 0x03 // network device.

#define DEV_FALSE FALSE
#define DEV_OK TRUE
#define DEV_NO_MEM 0x2
#define DEV_NO_DEVICE 0x3
#define DEV_BUSY 0x4
#define DEV_FREE 0x5

typedef void (*device_init_func)(void);

// Initialize all devices.
extern void init_device(void);

// Char device register.
extern int32 char_device_register(device_t *dev,  // Pointer to device node
                                   const char_t *Name,    // Device name
                                   char_t Mid,            // Main ID
                                   char_t Sid,            // Secondary ID
                                   void (*release)(void));// Callback function

extern void char_device_unregister(uint32 chdevMID, // Main ID
                                   uint32 chdevSID);// Secondary ID


// Init the device struct.
extern void device_init(device_t *Self);

// Open a device.
extern device_t *device_open(const char_t *name);

// Close a device.
extern void device_close(device_t *Self);

extern void device_lock(device_t *Self);
extern void device_unlock(device_t *Self);


#endif

